<script setup>
import {requestChangePassword} from "@/api";
import {reactive} from "vue";

const beforeClose = (action) =>
    new Promise((resolve) => {
      if (action === 'confirm') {
        resolve(true);
        dialogGroup.show = false;
      } else {
        // 拦截取消操作
        resolve(false);
      }
    })

const dialogGroup = reactive({
  title: "修改密码",
  message: "默认",
  show: false,
})

const storageGroup = reactive({
  userinfo:uni.getStorageSync("userInfo"),
})

const change = reactive({
  old_password: "",
  new_password: "",
  repeat_new_password: "",
  async changePassword() {
    if (this.new_password === this.repeat_new_password) {
      let result = await requestChangePassword({account: storageGroup.userinfo.account, old_password: this.old_password, new_password: this.new_password})
      if (result.statusCode === 200) {
        if (result.data === "DataFormatError") {
          dialogGroup.message = "密码格式错误";
        } else if (result.data === "EmptyError") {
          dialogGroup.message = "密码为空";
        } else if (result.data === "Normal") {
          dialogGroup.message = "修改成功";
        } else if (result.data === "DatabaseError") {
          dialogGroup.message = "数据库错误";
        } else if (result.data === "RepeatError") {
          dialogGroup.message = "新旧密码重复";
        } else if (result.data === "DataNotFoundError") {
          dialogGroup.message = "找不到账号";
        } else {
          dialogGroup.message = "服务器错误，请联系管理员";
        }
      }
    } else {
      dialogGroup.message = "新旧密码重复";
    }
    dialogGroup.show = true;
  }
})

</script>

<template>
  <van-dialog
      :before-close="beforeClose"
      :message="dialogGroup.message"
      :show="dialogGroup.show"
      :title="dialogGroup.title"
      custom-class="my-custom-class"
  />
  <scroll-view scroll-y="true">
    <view class="change-password">
      <view class="change-password-title-box">
        <view class="change-password-title">
          <text class="change-password-title-text">修改密码</text>
        </view>
      </view>
      <view class="change-password-input-box">
        <view class="change-password-input">
          <view class="change-password-input-block">
            <text class="change-password-input-text">旧密码:</text>
            <input v-model="change.old_password" class="change-password-input-username" placeholder="请输入您的旧密码"
                   type="password"/>
          </view>
          <br/>
          <view class="change-password-input-block">
            <text class="change-password-input-text">新密码:</text>
            <input v-model="change.new_password" class="change-password-input-account" placeholder="请输入您的新密码"
                   type="password"/>
          </view>
          <br/>
          <view class="change-password-input-block">
            <text class="change-password-input-text">新密码:</text>
            <input v-model="change.repeat_new_password" class="change-password-input-password" placeholder="重复一遍新密码"
                   type="password"/>
          </view>
        </view>
      </view>
      <view class="change-password-btn-box">
        <van-button class="change-password-btn" type="default" @click="change.changePassword()">修改密码</van-button>
      </view>
    </view>
  </scroll-view>
</template>

<style scoped src="../../css/change-password.css">

</style>